package com.linxin.controller;

import com.linxin.entity.Admin;
import com.linxin.entity.AdminLoginParam;
import com.linxin.service.AdminService;
import com.linxin.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.security.PublicKey;

@RestController
public class LoginController {
    @Autowired
    private AdminService adminService;
    @PostMapping("/login")
    public Result login(@RequestBody AdminLoginParam adminLoginParam) {
        return adminService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword());
    }
    //获取当前用户的信息
    @PostMapping("/admin/info")
    public Result AdminInfo(Principal principal){
        if (null == principal) {
            return null;
        }
        String username = principal.getName();
        Admin admin = adminService.getAdminByUserName(username); // 新建方法 getAdminByUserName 根据用户名获取用户
        admin.setPassword(null); // 安全起见，不给前端返回用户密码
        admin.setRoles(adminService.getRoles(admin.getId())); // 获取登录用户对应的角色列表
        return Result.succ(admin);
    }


}
